package com.megvii.corelib.update.task;

import com.megvii.corelib.update.UpdateUtil;

import java.net.HttpURLConnection;

/**
 * Created by linyue on 16/3/4.
 */
public class CheckSizeTask implements Runnable
{
    public String urlString;

    public int rangeStart;

    private CheckSizeListener listener;

    public CheckSizeTask(String urlString, int rangeStart)
    {
        super();
        this.urlString = urlString;
        this.rangeStart = rangeStart;
    }

    @Override
    public void run()
    {
        HttpURLConnection connection = UpdateUtil.createConnection(urlString, rangeStart);
        int size = -1;
        if (connection != null)
        {
            size = connection.getContentLength();
        }
        else
        {
            size = -1;
        }
        connection.disconnect();
        if (listener != null)
        {
            listener.onCheckSizeResult(this, size);
        }
    }

    public void setListener(CheckSizeListener listener)
    {
        this.listener = listener;
    }

    public interface CheckSizeListener
    {
        public void onCheckSizeResult(CheckSizeTask task, int size);
    }
}
